﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;

namespace YArchitech.HYSpace
{
	public class CmdSetNewThick : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				if (cmdData == null)
				{
					throw new System.ArgumentNullException();
				}
				RevitVersionFuncs.ClearSelection(cmdData.Application.ActiveUIDocument.Selection);
				bool flag = true;
				WallType wallType = this.m_refWall.WallType;
				CompoundStructure compoundStructure = wallType.GetCompoundStructure();
				IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
				foreach (CompoundStructureLayer compoundStructureLayer in layers)
				{
                    if ((int)compoundStructureLayer.Function == 3)
					{
						flag = false;
						compoundStructureLayer.Width = this.m_dNewThick / 304.8;
					}
				}
				if (flag && this.m_dNewThick > 0.0)
				{
					layers.Insert(0, new CompoundStructureLayer
					{
                        Function = (MaterialFunctionAssignment)3,
						Width = this.m_dNewThick / 304.8
					});
				}
				compoundStructure.SetLayers(layers);
				wallType.SetCompoundStructure(compoundStructure);
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public Wall m_refWall;

		public double m_dNewThick;
	}
}
